//
//  PHKitTool.m
//  camera
//
//  Created by liuyi on 5/19/16.
//  Copyright © 2016 liuyi. All rights reserved.
//

#import "PHKitTool.h"

@interface PHKitTool ()

@end

@implementation PHKitTool

#pragma mark - 获取所有的图片
+ (NSArray *)getAllImageGroup{
    //条件对象
    PHFetchOptions * allPhotosOptions = [[PHFetchOptions alloc]init];
    
    //降序排列
    allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
    
    //所有图片的集合
    PHFetchResult * allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
    
    NSArray *allArray = (NSArray *)allPhotos;
    

    return allArray;
    
}

#pragma mark - 获取相册中的照片
+ (NSArray *)getImageGroupFromAssetCollection:(PHAssetCollection *)assetCollection{
    PHFetchOptions *option = [[PHFetchOptions alloc]init];
    PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:assetCollection options:option];
    NSArray *imageGroup = (NSArray*)result;
    return imageGroup;
}


#pragma mark - 根据类型获取智能分组的图片结果
+ (NSArray *)getAssetCollectionGroupWithAssetSubType:(PHAssetCollectionSubtype)type{
    //条件对象
    PHFetchOptions * allPhotosOptions = [[PHFetchOptions alloc]init];
    
    //降序排列
    allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
    //智能分组图片集合
    PHFetchResult * smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:type options:nil];
    NSArray *array = (NSArray *)smartAlbums;
    return array;
}

#pragma mark - 把Asset 转换成Image
+ (UIImage *)getImageWithAsset:(PHAsset *)asset Size:(CGSize)size{
    UIImageView *imageV = [[UIImageView alloc]init];
    PHImageManager * imageManager = [[PHImageManager alloc]init];
    [imageManager requestImageForAsset:asset targetSize:size contentMode:1 options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        
        imageV.image = result;
        
    }];
    
    return imageV.image;
    
}



@end
